<?php

namespace App\Jobs;

use App\Models\Device;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\DB;

class SynRedisDe implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $redis = app("redis");
        $current_id = $redis ->get("maxdeid");
	if(empty($current_id)){
    $current_id = 0;
    }
       Device::where("id",">",$current_id)->chunk(10000 , function ($devices) use ($redis){
            $redis->pipeline(function ($pipe) use ($devices){
                foreach ($devices as $code){
                        $key = "device:list:{$code -> status}:{$code -> tp}" ;
                        $pipe -> lpush($key, $code -> sn);
                        $pipe->hmset("msg:device:{$code->sn}",$code -> toArray());
                }
                $pipe -> set("maxdeid",$code->id);
            });
        });
    }
}
